<template>
  <div style="padding: 10px">
    <!--  搜索区域-->
    <el-form :inline="true" class="demo-form-inline" style="margin: 10px 0">
      <el-form-item label="名称">
        <el-input
          type="text"
          placeholder="请输入名称"
          v-model="role.roleName"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="text"
          placeholder="请输入备注"
          v-model="role.roleDescribe"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="load" type="primary">查询</el-button>
        <el-button @click="add" type="primary">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      stripe
      style="width: 100%; height: 500px; overflow: auto"
    >
      <el-table-column
        prop="roleId"
        label="编号"
        sortable
        width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="roleName" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="roleDescribe" label="备注" align="center">
      </el-table-column>
      <el-table-column label="权限菜单" align="center">
        <template #default="scope">
          <el-select
            clearable
            v-model="scope.row.permissionIds"
            multiple
            placeholder="暂无，请选择"
            style="width: 80%"
          >
            <el-option
              v-for="item in permissions"
              :key="item.permissionId"
              :label="item.permissionRealName"
              :value="item.permissionId"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            size="mini"
            type="primary"
            @click="savePermission(scope.row)"
            >保存权限</el-button
          >
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.row.roleId)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="dialogVisible" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="form.roleName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.roleDescribe" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Role",
  components: {},
  data() {
    return {
      loading: true,
      form: {},
      dialogVisible: false,
      total: 0,
      tableData: [],
      role: {}, //存储查询信息
      permissions: [],
      title: "修改角色",
    };
  },
  created() {
    this.load();
  },
  methods: {
    savePermission(row) {
      request.put("/userRole/updatePermission", row).then((res) => {
        if (res.code === 0) {
          this.$message.success(res.msg);
          if (res.data) {
            this.$router.push("/login");
          }
        }
      });
    },
    load() {
      this.loading = true;
      request.post("/userRole/page", JSON.stringify(this.role)).then((res) => {
        this.loading = false;
        this.tableData = res.data.list;
        this.total = res.data.totalElements;
      });

      //获取权限列表
      request.post("/permission/page").then((res) => {
        console.log(res);
        this.permissions = res.data.list;
      });
    },
    add() {
      this.dialogVisible = true;
      this.title = "新增角色";
      this.form = {};
    },
    save() {
      if (this.form.roleId) {
        // 更新
        request.put("/userRole/", this.form).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: "success",
              message: "更新成功",
            });
          }
          this.load(); // 刷新表格的数据
          this.dialogVisible = false; // 关闭弹窗
        });
      } else {
        // 新增
        request.post("/userRole/", this.form).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: "success",
              message: "新增成功",
            });
          }
          this.load(); // 刷新表格的数据
          this.dialogVisible = false; // 关闭弹窗
        });
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.title = "修改角色";
      this.dialogVisible = true;
    },
    handleDelete(roleId) {
      request.delete("/userRole/" + roleId).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        }
        this.load(); // 删除之后重新加载表格的数据
      });
    },
  },
};
</script>
